package com.laxcen.basiclib.core.crashpush;

import android.util.Log;

import com.laxcen.basiclib.BaseApp;
import com.laxcen.basiclib.core.bridgewebview.bean.PushLog;
import com.laxcen.basiclib.core.broadcast.BroadcastManager;
import com.laxcen.basiclib.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.sentry.Sentry;
import io.sentry.SentryEvent;
import io.sentry.protocol.SentryException;

/**
 *  进一步封装异常处理类
 *  有节奏的推送异常数据，防止数据量过大
 *  造成服务器过载
 *   每间隔1秒 推送一条数据，分批推送
 */
public class CrashPushManager {
    private static CrashPushManager crashPushManager;
    public static CrashPushManager getInstance(){
        if(crashPushManager==null){
            synchronized (CrashPushManager.class){
                if(crashPushManager==null){
                    crashPushManager = new CrashPushManager();
                }
            }
        }
        return crashPushManager;
    }

    /**
     * 推送异常日志
     * @param pushLog
     */
    public void pushLog(PushLog pushLog){
        try {
            SentryEvent sentryEvent = new SentryEvent();
            sentryEvent.setLevel(pushLog.getLevel());
            Map<String,String> tags = new HashMap<>();
            tags.put("isAllSollapse",pushLog.isAllSollapse()+"");
            tags.put("isBlankInterface",pushLog.isBlankInterface()+"");
            sentryEvent.setTags(tags);
            sentryEvent.setRelease(UUID.randomUUID().toString());
            sentryEvent.setLogger(pushLog.getType());
            Sentry.captureEvent(sentryEvent);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(pushLog.isAllSollapse()||pushLog.isBlankInterface()){
            String msg = pushLog.getMessage();
            if(StringUtil.isEmpty(msg)){
                msg= pushLog.getThrowable().getMessage();
            }
            BaseApp.getApp().sendCaughtMessage("程序运行出现崩溃",msg);
        }
    }
}
